package cn.weekimwee.map3d.wkw;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;

import java.util.ArrayList;
import java.util.List;

import cn.weekimwee.map3d.R;

/**
 * Created by Wee Kim Wee on 2017/11/19.
 */

public class WKWDrivingRouteOverlay {

    private AMap aMap;
    private List<LatLng> drivePath;
    private float width = 25;
    private Marker startMarker;
    private Marker endMarker;
    private BitmapDescriptor startBit;
    private BitmapDescriptor endBit;
    private List<Polyline> allPolyLines = new ArrayList();
    private boolean isFirst;
    private PolylineOptions polyLineOptions;
    private LatLng startLatLng;
    private LatLng endLatLng;

    /**
     * 构造方法：司机刚接到单，未接到人时候用的构造方法，要画线
     *
     * @param aMap      地图
     * @param drivePath 从车辆点到接人点的规划路径的经纬度集合
     */
    public WKWDrivingRouteOverlay(AMap aMap, List<LatLng> drivePath) {
        this.aMap = aMap;
        this.drivePath = drivePath;
        this.startLatLng = drivePath.get(0);
        this.endLatLng = drivePath.get(drivePath.size() - 1);
        this.isFirst = true;

        polyLineOptions = new PolylineOptions();
        polyLineOptions.width(width);
        polyLineOptions.setCustomTexture(BitmapDescriptorFactory.fromResource(R.drawable.custtexture));
        endBit = BitmapDescriptorFactory.fromResource(R.drawable.amap_start);
        addMarkerToMap();
        drawLine();
    }

    /**
     * 构造方法：司机已接到人，不画线
     *
     * @param aMap        地图
     * @param startLatLng 起点经纬度
     * @param endLatLng   终点经纬度
     */
    public WKWDrivingRouteOverlay(AMap aMap, LatLng startLatLng, LatLng endLatLng) {
        this.aMap = aMap;
        this.startLatLng = startLatLng;
        this.endLatLng = endLatLng;
        this.isFirst = false;

        startBit = BitmapDescriptorFactory.fromResource(R.drawable.amap_start);
        endBit = BitmapDescriptorFactory.fromResource(R.drawable.amap_end);
        addMarkerToMap();
    }

    /**
     * 将起点终点的marker添加到地图上
     */
    public void addMarkerToMap() {
        try {
            if (isFirst) {
                endMarker = aMap.addMarker(new MarkerOptions().position(drivePath.get(drivePath.size() - 1)).icon(endBit));
            } else {
                startMarker = aMap.addMarker(new MarkerOptions().position(startLatLng).icon(startBit));
                endMarker = aMap.addMarker(new MarkerOptions().position(endLatLng).icon(endBit));
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    /**
     * 画线
     */
    private void drawLine() {
        try {
            for (LatLng latLng : drivePath) {
                polyLineOptions.add(latLng);
            }
            allPolyLines.add(aMap.addPolyline(polyLineOptions));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /**
     * 将点始终保持在能看到的位置
     */
    public void zoomToSpan() {
        try {
            LatLngBounds.Builder b = LatLngBounds.builder();
            b.include(new LatLng(startLatLng.latitude,startLatLng.longitude));
            b.include(new LatLng(endLatLng.latitude,endLatLng.longitude));
            LatLngBounds bounds = b.build();
            aMap.animateCamera(CameraUpdateFactory.newLatLngBoundsRect(bounds, 100, 100, 400, 350));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /**
     * onDestory
     */
    public void onDestroy() {
        if (startBit != null) startBit.recycle();
        if (endBit != null) endBit.recycle();
        if (startMarker != null) startMarker.destroy();
        if (endMarker != null) endMarker.destroy();
        if (allPolyLines.size() != 0) {
            for (Polyline line : allPolyLines) {
                line.remove();
            }
        }
    }
}
